<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/6
 * Time: 20:55
 *
 * 数据查询到表格
 */


require_once "../../php/config/config.php";
require_once "../model/adminDb.php";

$res = array();
$action = $_POST['action'];
switch ($action) {
    case "table":
        table();
        break;
    case "time":
        getTimeBug();
        break;
    default :
}

function table()
{

    $AdminDb = new AdminDb();
    $product = $_POST['product'];
    $person = $_POST['person'];
    $status = $_POST['status'];
    $auther = $_POST['auther'];
    $page = $_POST['page'];//当前页
    $nextPage = $page + 1;//下一页
    $limit = $_POST['limit'];//限制查询数据的数量
    $firstNum = ($page - 1) * $limit;
    $lastNum = $page * $limit;
    $name = $_SESSION['name'];
    $userData = $AdminDb->query_userName($name);
    $card = $userData[0]['card'];

    if ($card == 1) {//管理员

        if ($product == "0" && $person == "0" && $status == "all" && $auther == "0") {//当未选择产品、负责人、状态
            //查询所有数据，无初始默认查询
            $data = $AdminDb->query_bug();
            $bugData = $AdminDb->query_bug_page($firstNum, $lastNum);
        } elseif ($product != "0" && $person == "0" && $status == "all" && $auther == "0") {//产品
            $data = $AdminDb->query_bug_Product($product);
            $bugData = $AdminDb->query_bug_Product_page($firstNum, $lastNum, $product);
        } elseif ($person != "0" && $product == "0" && $status == "all" && $auther == "0") {//负责人
            $data = $AdminDb->query_bug_person($person);
            $bugData = $AdminDb->query_bug_person_page($firstNum, $lastNum, $person);
        } elseif ($status != "all" && $product == "0" && $person == "0" && $auther == "0") {//状态
            $data = $AdminDb->query_bug_status($status);
            $bugData = $AdminDb->query_bug_status_page($firstNum, $lastNum, $status);
        } elseif ($status == "all" && $product == "0" && $person == "0" && $auther != "0") {//只点击了创建人
            $data = $AdminDb->query_bug_auther($auther);
            $bugData = $AdminDb->query_bug_auther_page($firstNum, $lastNum, $auther);
        } elseif ($product != "0" && $person != "0" && $status == "all" && $auther == "0") {//产品、负责人
            $data = $AdminDb->query_bug_pp($product, $person);
            $bugData = $AdminDb->query_bug_pp_page($firstNum, $lastNum, $person, $product);
        } elseif ($product != "0" && $status != "all" && $person == "0" && $auther == "0") {//产品、状态
            $data = $AdminDb->query_bug_ps($product, $status);
            $bugData = $AdminDb->query_bug_ps_page($firstNum, $lastNum, $status, $product);
        } elseif ($status != "all" && $person != "0" && $product == "0" && $auther == "0") {//状态、负责人
            $data = $AdminDb->query_bug_sp($status, $person);
            $bugData = $AdminDb->query_bug_sp_page($firstNum, $lastNum, $status, $person);
        } elseif ($status != "all" && $product != "0" && $person != "0" && $auther == "0") {//
            $data = $AdminDb->query_bug_all($product, $person, $status);
            $bugData = $AdminDb->query_bug_all_page($firstNum, $lastNum, $status, $person, $product);
        } //当点击创建人之后的操作--单个
        elseif ($status != "all" && $product == "0" && $person == "0" && $auther != "0") {
            $data = $AdminDb->query_bug_sa($status, $auther);
            $bugData = $AdminDb->query_bug_sa_page($firstNum, $lastNum, $status, $auther);
        } elseif ($status == "all" && $product != "0" && $person == "0" && $auther != "0") {
            $data = $AdminDb->query_bug_pa($product, $auther);
            $bugData = $AdminDb->query_bug_pa_page($firstNum, $lastNum, $product, $auther);
        } elseif ($status == "all" && $product == "0" && $person != "0" && $auther != "0") {
            $data = $AdminDb->query_bug_ppa($person, $auther);
            $bugData = $AdminDb->query_bug_ppa_page($firstNum, $lastNum, $person, $auther);
        } //两个
        elseif ($status != "all" && $product != "0" && $person == "0" && $auther != "0") {
            $data = $AdminDb->query_bug_spa($status, $product, $auther);
            $bugData = $AdminDb->query_bug_spa_page($firstNum, $lastNum, $status, $product, $auther);
        } elseif ($status != "all" && $product == "0" && $person != "0" && $auther != "0") {
            $data = $AdminDb->query_bug_sppa($status, $person, $auther);
            $bugData = $AdminDb->query_bug_sppa_page($firstNum, $lastNum, $status, $person, $auther);
        } elseif ($status == "all" && $product != "0" && $person != "0" && $auther != "0") {
            $data = $AdminDb->query_bug_pppa($person, $product, $auther);
            $bugData = $AdminDb->query_bug_pppa_page($firstNum, $lastNum, $person, $product, $auther);
        } elseif ($status != "all" && $product != "0" && $person != "0" && $auther != "0") {
            $data = $AdminDb->query_bug_sppall($status, $product, $person, $auther);
            $bugData = $AdminDb->query_bug_spall_page($firstNum, $lastNum, $status, $product, $person, $auther);
        }
    } else {
        if ($product == "0" && $person == "0" && $status == "all" && $auther == "0") {//当未选择产品、负责人、状态
            //查询所有数据，无初始默认查询
            $data = $AdminDb->query_bug_user1($name);
            $bugData = $AdminDb->query_bug_page_user($name, $firstNum, $lastNum);
        } elseif ($product == "0" && $person == "0" && $status == "all" && $auther != "0") {
            $data = $AdminDb->query_bug_user11($name);
            $bugData = $AdminDb->query_bug_page_user1($name, $firstNum, $lastNum);
        } elseif ($product != "0" && $person == "0" && $status == "all" && $auther == "0") {//产品
            $data = $AdminDb->query_bug_Product1($name, $product);
            $bugData = $AdminDb->query_bug_Product_page1($name, $firstNum, $lastNum, $product);
        } elseif ($person != "0" && $product == "0" && $status == "all" && $auther == "0") {//负责人
            $data = $AdminDb->query_bug_person1($name, $person);
            $bugData = $AdminDb->query_bug_person_page1($name, $firstNum, $lastNum, $person);
        } elseif ($status != "all" && $product == "0" && $person == "0" && $auther == "0") {//状态
            $data = $AdminDb->query_bug_status1($name, $status);
            $bugData = $AdminDb->query_bug_status_page1($name, $firstNum, $lastNum, $status);
        } elseif ($product != "0" && $person != "0" && $status == "all" && $auther == "0") {//产品、负责人
            $data = $AdminDb->query_bug_pp1($name, $product, $person);
            $bugData = $AdminDb->query_bug_pp_page1($name, $firstNum, $lastNum, $person, $product);
        } elseif ($product != "0" && $status != "all" && $person == "0" && $auther == "0") {//产品、状态
            $data = $AdminDb->query_bug_ps1($name, $product, $status);
            $bugData = $AdminDb->query_bug_ps_page1($name, $firstNum, $lastNum, $status, $product);
        } elseif ($status != "all" && $person != "0" && $product == "0" && $auther == "0") {//状态、负责人
            $data = $AdminDb->query_bug_sp1($name, $status, $person);
            $bugData = $AdminDb->query_bug_sp_page1($name, $firstNum, $lastNum, $status, $person);
        } elseif ($status != "all" && $product != "0" && $person != "0" && $auther == "0") {//
            $data = $AdminDb->query_bug_all1($name, $product, $person, $status);
            $bugData = $AdminDb->query_bug_all_page1($name, $firstNum, $lastNum, $status, $person, $product);
        } elseif ($status != "all" && $product == "0" && $person == "0" && $auther != "0") {//只点击了创建人
            $data = $AdminDb->query_bug_auther($auther, $status);
            $bugData = $AdminDb->query_bug_auther_page($firstNum, $lastNum, $auther);
        } //当点击创建人之后的操作--单个
        elseif ($status != "all" && $product == "0" && $person == "0" && $auther != "0") {
            $data = $AdminDb->query_bug_sa($status, $auther);
            $bugData = $AdminDb->query_bug_sa_page($firstNum, $lastNum, $status, $auther);
        } elseif ($status == "all" && $product != "0" && $person == "0" && $auther != "0") {
            $data = $AdminDb->query_bug_pa($product, $auther);
            $bugData = $AdminDb->query_bug_pa_page($firstNum, $lastNum, $product, $auther);
        } elseif ($status == "all" && $product == "0" && $person != "0" && $auther != "0") {
            $data = $AdminDb->query_bug_ppa($person, $auther);
            $bugData = $AdminDb->query_bug_ppa_page($firstNum, $lastNum, $person, $auther);
        } //两个
        elseif ($status != "all" && $product != "0" && $person == "0" && $auther != "0") {
            $data = $AdminDb->query_bug_spa($status, $product, $auther);
            $bugData = $AdminDb->query_bug_spa_page($firstNum, $lastNum, $status, $product, $auther);
        } elseif ($status != "all" && $product == "0" && $person != "0" && $auther != "0") {
            $data = $AdminDb->query_bug_sppa($status, $person, $auther);
            $bugData = $AdminDb->query_bug_sppa_page($firstNum, $lastNum, $status, $person, $auther);
        } elseif ($status == "all" && $product != "0" && $person != "0" && $auther != "0") {
            $data = $AdminDb->query_bug_pppa($person, $product, $auther);
            $bugData = $AdminDb->query_bug_pppa_page($firstNum, $lastNum, $person, $product, $auther);
        } elseif ($status != "all" && $product != "0" && $person != "0" && $auther != "0") {
            $data = $AdminDb->query_bug_sppall($status, $product, $person, $auther);
            $bugData = $AdminDb->query_bug_spall_page($firstNum, $lastNum, $status, $product, $person, $auther);
        } else {
            $data = $AdminDb->query_bug_user($name);
            $bugData = $AdminDb->query_bug_user_page($name, $firstNum, $lastNum);
        }
    }

    $sum = count($data);
    try {
        $result = $data;
        if ($result) {
            $dat = array(
                'code' => 0,
                "name" => $card,
                'count' => $sum,
                'data' => $bugData,
                'msg' => "查询成功",
            );
            echo json_encode($dat);
        } else {
            $dat = array(
                'code' => 0,
                'count' => $sum,
                'msg' => "查询失败",
            );
            echo json_encode($dat);
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

//根据时间查询
function getTimeBug()
{

    $start_time = $_POST['startTime'];
    $end_time = $_POST['endTime'];
    $page = $_POST['page'];//当前页
    $nextPage = $page + 1;//下一页
    $limit = $_POST['limit'];//限制查询数据的数量
    $firstNum = ($page - 1) * $limit;
    $lastNum = $page * $limit;
    $AdminDb = new AdminDb();
    $name = $_SESSION['name'];
    $s = strtotime($start_time);
    $e = strtotime($end_time);//将日期转化为时间搓
    $bugData = $AdminDb->query_bug_time($s,$e,$name);


    if($bugData){
    $count=count($bugData);

        $data = $AdminDb->query_bug_time_page($s,$e,$name,$firstNum,$lastNum);

        if($data){
            $res["code"] = 0;
            $res['msg'] = "查询成功";
            $res['count']=$count;
            $res['data']=$data;
            $res['s']=$s;
            $res['e']=$e;
        }
        else{
            $res["code"] = 202;
            $res['msg'] = "无数据";
        }
    }
    else{
        $res["code"] = 103;
        $res['msg'] = "无数据";
    }
    echo json_encode($res);

}

